<template>
  <!-- 放置一个弹层 用来编辑新增节点 -->
  <el-dialog
    :title="`${showText}权限点`"
    :visible="showDialog"
    @close="btnCancel"
  >
    <!-- 表单 -->
    <el-form ref="perForm" :model="formData" :rules="rules" label-width="120px">
      <el-form-item label="权限名称" prop="name">
        <el-input v-model="formData.name" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限标识" prop="code">
        <el-input v-model="formData.code" style="width:90%" />
      </el-form-item>
      <el-form-item label="权限描述">
        <el-input v-model="formData.description" style="width:90%" />
      </el-form-item>
      <el-form-item label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
    </el-form>
    <el-row slot="footer" type="flex" justify="center">
      <el-col :span="6">
        <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        <el-button size="small" @click="btnCancel">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { updatePermissionAPI, addPermissionAPI } from '@/api/permission'

export default {
  name: 'AddPerm',
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    formData: {
      type: Object,
      required: true
    }
  },
  data() {
    return {
      showText: '添加',
      rules: {
        name: [{ required: true, trigger: 'blur', message: '请输入权限名' }],
        code: [{ required: true, trigger: 'blur', message: '请输入标识' }]
      }
    }
  },
  methods: {
    // 提交
    btnOK() {
      this.$refs.perForm
        .validate()
        .then(() => {
          if (this.formData.id) return updatePermissionAPI(this.formData)
          return addPermissionAPI(this.formData)
        })
        .then(() => {
          //  提示消息
          this.$message.success('新增成功')
          this.$emit('reload') // 刷新数据
          this.$emit('update:show-dialog', false)
        })
    },

    // 取消
    btnCancel() {
      this.$refs.perForm.resetFields()
      this.$emit('update:show-dialog', false)
    }
  }
}
</script>
